package com.xiaolun.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.xiaolun.mapper.UserMapper;
import com.xiaolun.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 硕鼠
 * @Date 2020/4/10 23:05
 * @Version 1.0
 * @Description 文件的插入操作，将前端数据存到数据库
 */
@Controller
public class UserInsert {
    @Autowired
    private UserMapper userMapper;

//    @Autowired
//    private User user;

    @RequestMapping("/addUserNum")
    @ResponseBody
    public void InsertTest(String title,String author,String note){
        System.out.println("title：" + title);
        System.out.println("author：" + author);
        System.out.println("note：" + note);
        //接收参数
        if ((title != null) && (author != null) && (note != null)){
            User user = new User();
            user.setTitle(title);
            user.setAuthor(author);
            user.setNote(note);
            int result = this.userMapper.addUser(user);
            System.out.println("数据变换条数：" + result);
        } else {
            System.out.println("前端传过来数据为空");
        }
    }

    @RequestMapping("/newsList")
    @ResponseBody
    public Map<String,Object> newsList(){
        //接收参数
        Map<String,Object> map = new HashMap<>();//放在这里
        List <User> list  = this.userMapper.findAll(); //找到所有数据
        map.put("data",list); //取得数据
        map.put("count",list.size()); //取得数据量
        map.put("code",0); //取得数据
        return map;
    }
}
